<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=
    , initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        body{
            background: black;
            text-align: center;
        }
        #c1{
            background: #fff;
        }
    </style>
    <script>
        window.onload=function(){
            let oC=document.getElementById('c1');
            let btn1=document.getElementById('btn1');
            let gd=oC.getContext('2d');
            let img=new Image();
            img.src='2.jpg';
            img.onload=()=>{
                gd.drawImage(img,0,0)
                let width=oC.width;
                let imageData=gd.getImageData(0,0,width,oC.height);
                // alert(`r:${imageData.data[0]}`)
                for(let r=0;r<oC.height;r++){
                    for(let c=0;c<width;c++){
                        let avg=(imageData.data[(r*width+c)*4]+imageData.data[(r*width+c)*4+1]+imageData.data[(r*width+c)*4+2])/3;
                        imageData.data[(r*width+c)*4]=imageData.data[(r*width+c)*4+1]=imageData.data[(r*width+c)*4+2]=avg;

                    }
                }
                gd.putImageData(imageData,0,0)
            }
            btn1.onclick=()=>{
                let str=oC.toDataURL();
            }

        }
    </script>
</head>
<body>
    <input type="button" id="btn1">
    <canvas id="c1" width="800" height="600">
        您的浏览器,不支持canvas
    </canvas>
</body>
</html>